package com.yumeng.framework.auth.helper;

import com.yumeng.common.api.ApiCode;
import com.yumeng.common.helper.AssertHelper;
import com.yumeng.common.runtime.ThreadLocalHelper;
import com.yumeng.common.utils.HttpUtils;
import com.yumeng.framework.auth.bean.TokenInfo;
import com.yumeng.framework.auth.exception.TokenAuthException;
import com.yumeng.config.helper.PropHelper;
import com.yumeng.config.properties.YumengAuthProperties;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wxd
 * @date 2025/9/24 09:57
 */
@Slf4j
public class HttpTokenHelper {

    private static String tokenName;
    private static String refreshTokenName;
    private static YumengAuthProperties.JwtProperties getJwtProperties(){
        return PropHelper.getJwtProperties();
    }

    /**
     * 获取token名称
     * @return
     */
    public static String getTokenName() {
        if (StringUtils.isBlank(tokenName)){
            tokenName = getJwtProperties().getTokenName();
            AssertHelper.assertNotBlank(tokenName, "http head tokenName is blank");
            log.debug("tokenName:{}", tokenName);
        }
        return tokenName;
    }

    /**
     * 获取RefreshToken名称
     * @return
     */
    public static String getRefreshTokenName() {
        if (StringUtils.isBlank(refreshTokenName)){
            refreshTokenName = getJwtProperties().getRefreshTokenName();
            AssertHelper.assertNotBlank(refreshTokenName, "http head refreshTokenName is blank");
            log.debug("refreshTokenName:{}", refreshTokenName);
        }
        return refreshTokenName;
    }

    public static boolean hasToken(){
        return StringUtils.isNotBlank(getToken());
    }

    public static boolean hasToken(HttpServletRequest request){
        return StringUtils.isNotBlank(getToken(request));
    }

    public static String getToken() {
        return getToken(HttpUtils.getRequest());
    }
    /**
     * 从请求头中 获取token
     * @param request
     * @return
     */
    public static String getToken(HttpServletRequest request) {
        if (request == null) {
            throw new TokenAuthException("request不能为空");
        }
        // 从请求头中获取token
        return request.getHeader(getTokenName());
    }

    public static String getRefreshToken() {
        return getRefreshToken(HttpUtils.getRequest());
    }
    /**
     * 从请求头中 获取refreshToken
     * @param request
     * @return
     */
    public static String getRefreshToken(HttpServletRequest request) {
        if (request == null) {
            throw new TokenAuthException("request不能为空");
        }
        // 从请求头中获取token
        return request.getHeader(getRefreshTokenName());
    }

    /**
     * 刷新响应头的token
     * @param tokenInfo
     * @param code
     * @param response
     */
    public static void refreshHeaderToken(TokenInfo tokenInfo, ApiCode code, HttpServletResponse response){
        if (code != null){
            ThreadLocalHelper.setApiCode(code);
        }
        response.setHeader(getTokenName(), tokenInfo.getToken());
        if (StringUtils.isNotBlank(tokenInfo.getRefreshToken())){
            response.setHeader(getRefreshTokenName(), tokenInfo.getRefreshToken());
        }
    }

    /**
     * 刷新响应头的token
     * @param tokenInfo
     * @param response
     */
    public static void refreshHeaderToken(TokenInfo tokenInfo, HttpServletResponse response){
        refreshHeaderToken(tokenInfo, null, response);
    }
}
